package com.cckj.projectapi.framework.config.security;

import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class MyUserDetailsService implements UserDetailsService {

    /**
     * 登陆位置逻辑，会进入到此流程中
     * // 用户验证
     * Authentication authentication;
     * try {
     * authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(
     * vo.getUsername(), vo.getPassword()));
     * } catch (BadCredentialsException e) {
     * log.error("密码错误", e);
     * throw new BusinessException("密码错误");
     * } catch (AuthenticationException e) {
     * log.error("认证失败", e);
     * if (e.getCause() instanceof BusinessException) {
     * throw new BusinessException(e.getMessage());
     * } else {
     * throw new BusinessException("登陆失败");
     * }
     * }
     * SecurityUser principal = (SecurityUser) authentication.getPrincipal();
     */
    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        // 用户信息

        // 用户角色列表

        // 用户菜单权限列表

        List<String> authorities = Lists.newArrayList();
        List<GrantedAuthority> grantedAuthorities = AuthorityUtils.commaSeparatedStringToAuthorityList(
                String.join(",", authorities)
        );
        return new SecurityUser(grantedAuthorities, null);
    }
}